Deavmi's coding shack
Commit 3c34a8ebd9f7941f8686b9094b8d16094e8fa0aa
Parents : c3b0d06
Author : Tristan Brice Velloza Kildaire <deavmi@redxen.eu>
Date : 2026-04-19T21:06:38+02:00
Handle EOF properl;y
Changes
Diff
diff --git a/core.go b/core.go
index 5f2c220..cc56e7e 100644
--- a/core.go
+++ b/core.go
@@ -1,6 +1,7 @@
package ini
import (
+ "errors"
"fmt"
"io"
@@ -41,6 +42,10 @@ type Section struct {
parent *Ini
}
+func (s Section) String() string {
+ return fmt.Sprintf("%s (%v)", s.name, s.records)
+}
+
// represents a key-value
// pair within a section
type Record struct {
@@ -56,6 +61,10 @@ type Record struct {
parent *Section
}
+func (r Record) String() string {
+ return fmt.Sprintf("(key=%s, value=%v)", r.name, r.value)
+}
+
// creates a new record
func NewRecord(name string, value string) {
@@ -103,7 +112,9 @@ func (p *Parser) Parse() (*Ini, error) {
for {
r, e := p.c.CurrentCharacter()
- if e != nil {
+ if errors.Is(e, io.EOF) { // EOF is allowed here
+ break
+ } else if e != nil {
return types.Zero[*Ini](), e
}
@@ -129,7 +140,7 @@ func (p *Parser) Parse() (*Ini, error) {
log.Debug("Section derived: %v", s)
s.parent = &p.ini // parent
- p.sc_ls = append(p.sc_ls, &s)
+ p.ini.sections = append(p.ini.sections, &s)
}
}
@@ -152,7 +163,9 @@ func (p *Parser) parseSection() (Section, error) {
// character of the next section (i.e. a `[`)
for {
r, e := p.c.CurrentCharacter()
- if e != nil {
+ if errors.Is(e, io.EOF) { // EOF is allowed here
+ break
+ } else if e != nil { // any other error is not allowed
return types.Zero[Section](), e
}
@@ -186,6 +199,7 @@ func (p *Parser) parseSection() (Section, error) {
}
}
+ log.Debug("e before leave: %v", e)
log.Trace("Parsed section '%v'", sec)
return sec, nil
}
@@ -232,9 +246,14 @@ func (p *Parser) parseRecord(sec *Section) (Record, error) {
// skips all whitespace too
func (p *Parser) parseRune() (string, error) {
var out_r rune
+ var gotRune bool = false
for {
r, e := p.c.CurrentCharacter()
if e != nil {
+ // EOF here is allowed IF we have the rune already
+ if errors.Is(e, io.EOF) && gotRune {
+ break
+ }
return types.Zero[string](), e
}
@@ -244,6 +263,7 @@ func (p *Parser) parseRune() (string, error) {
} else {
out_r = r
p.c.Next() // consume the rune
+ gotRune = true
break
}
}
@@ -256,6 +276,10 @@ func (p *Parser) parseString(breaker rune) (string, error) {
for {
r, e := p.c.CurrentCharacter()
if e != nil {
+ // EOF here is fine if we got some string
+ if errors.Is(e, io.EOF) && len(strBuildUp) > 0 {
+ break
+ }
return types.Zero[string](), e
}
@@ -295,6 +319,7 @@ func (p *Parser) parseSection_header() (string, error) {
for {
r, e := p.c.CurrentCharacter()
if e != nil {
+ // Note, an EOF here would be BAD
return types.Zero[string](), e
}
@@ -342,7 +367,12 @@ func (p *Parser) parseComment() (string, error) {
var comment string
for {
r, e := p.c.CurrentCharacter()
- if e != nil {
+ // EOF is allowed here (we don't care how 'big'
+ // the comment is - it could be zero characters
+ // for all we care)
+ if errors.Is(e, io.EOF) {
+ return comment, nil
+ } else if e != nil {
return types.Zero[string](), e
}
Served by rngit 1.5.0 - Generated in 0.07s